using System;
using UnityEngine;
using UnityEngine.Advertisements;
using MinesweeperPlus.Scripts.Abstractions.Interfaces;

namespace MinesweeperPlus.Scripts.Objects.Advertisements
{
    public sealed class RewardedAds : IUnityAdsLoadListener, IUnityAdsShowListener, IAds
    {
        #region Constants
        private const string ANDROID_ADS_ID = "Rewarded_Android";
        private const string IOS_ADS_ID = "Rewarded_iOS";
        #endregion

        #region Actions
        public Action OnAdsShowComplete { get; set; }
        #endregion

        #region Variables
        private string _adsId;
        #endregion

        #region RewardedAds Methods
        public RewardedAds()
        {
            _adsId = 
                (Application.platform == RuntimePlatform.IPhonePlayer) ? IOS_ADS_ID : ANDROID_ADS_ID;  
            LoadAds();  
        }

        public void LoadAds()
        {
            Advertisement.Load(_adsId, this);
        }

        public void ShowAds()
        {
            Advertisement.Show(_adsId, this);
        }

        public void OnUnityAdsAdLoaded(string placementId)
        {
            Debug.Log($"{nameof(OnUnityAdsAdLoaded)}({placementId})");
        }
        public void OnUnityAdsFailedToLoad(string placementId, UnityAdsLoadError error, string message)
        {
            Debug.Log($"{nameof(OnUnityAdsAdLoaded)}({placementId}) : {error.ToString()} - {message}");
        }
        public void OnUnityAdsShowFailure(string placementId, UnityAdsShowError error, string message)
        {
            Debug.Log($"{nameof(OnUnityAdsShowFailure)}({placementId}) : {error.ToString()} - {message}");
        }
        public void OnUnityAdsShowStart(string placementId)
        {
            Debug.Log($"{nameof(OnUnityAdsShowStart)}({placementId})");
        }
        public void OnUnityAdsShowClick(string placementId)
        {
            Debug.Log($"{nameof(OnUnityAdsShowClick)}({placementId})");
        }
        public void OnUnityAdsShowComplete(string placementId, UnityAdsShowCompletionState state)
        {
            if (state.Equals(UnityAdsShowCompletionState.COMPLETED))
                OnAdsShowComplete?.Invoke();
        }
        #endregion
    }
}
